package com.bing.bingPro.service;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.org.atool.fluent.mybatis.base.crud.IBaseQuery;
import com.bing.bingPro.entity.TestDetailsEntity;
import com.bing.bingPro.entity.TestStatusEntity;
import com.bing.bingPro.mapper.TestDetailsMapper;
import com.bing.bingPro.mapper.TestStatusMapper;
import com.bing.bingPro.wrapper.TestDetailsQuery;
import com.bing.bingPro.wrapper.TestStatusQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.bing.bingPro.service.SendMailService.isEmail;

@Service
@Slf4j
public class TestDetailsService {

    private final TestDetailsMapper testDetailsMapper;
    private final TestStatusMapper testStatusMapper;
    private final SendMailService sendMailService;
    private final ProTerminalTestSaveService proTerminalTestSaveService;
    private final VendorEmailService vendorEmailService;

    public TestDetailsService(TestDetailsMapper testDetailsMapper, TestStatusMapper testStatusMapper, SendMailService sendMailService, ProTerminalTestSaveService proTerminalTestSaveService, VendorEmailService vendorEmailService) throws ParseException {
        this.testDetailsMapper = testDetailsMapper;
        this.testStatusMapper = testStatusMapper;
        this.sendMailService = sendMailService;
        this.proTerminalTestSaveService = proTerminalTestSaveService;
        this.vendorEmailService = vendorEmailService;
    }

    // 获取当前时间
    public Date getNowtime() throws ParseException {
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(TIMEFORMAT); // 获取当前日期
        DateFormat fmt = new SimpleDateFormat(TIMEFORMAT);
        return fmt.parse(date.format(formatter));
    }


    // 新建/获取 文件保存路径 默认为项目根目录下的files目录
    public String getFileDir() throws IOException {
        File fileTmp = new File("");
        File file = new File(String.format("%s/files/", fileTmp.getCanonicalPath()));
        if (!file.exists()) {
            boolean result = file.mkdirs();
            if (result) {
                Console.log(String.format("新建文件保存路径成功:%s ", file.getCanonicalPath()));
                return file.getCanonicalPath();
            }
        }
        Console.log(String.format("文件保存路径已存在:%s ", file.getCanonicalPath()));
        return file.getCanonicalPath();
    }

    private static final String TIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DATALIST = "datalist";
    private static final String PROVINCE = "province";
    private static final String WUYEARDATA = "无该年份数据！";
    private static final String TOTAL = "total";
    private static final String RELATEDID = "related_id";
    private static final String VENDOROVER = "厂家自测完成";
    private static final String VERSIONCODE = "待厂家自测";
    private static final String TESTERR = "测试未通过";
    private static final String EMAILMSG = "你好!省级终端版本提测中省份为：%s厂商为：%s版本为：%s测试结果为不通过。请知悉！";
    private static final String IDLIST = "idList";
    private static final String TESTLIST = "testinfolist";
    private static final String PROVINCETEST = "待省内测试";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIMEFORMAT);//注意月份是MM
    Date sBaseDate = simpleDateFormat.parse("2021-01-01 00:00:00");
    Date e21date = simpleDateFormat.parse("2021-12-31 23:59:59");
    Date e22date = simpleDateFormat.parse("2022-12-31 23:59:59");
    Date e23date = simpleDateFormat.parse("2023-12-31 23:59:59");
    String[] qArgs = new String[]{"测试通过", "未发货无需测试"};

    // 计算全国通过率和可部署情况
    public void statisticsPassRate(String year) {
        if (year.equals("2022")) {
            List<Object> versionList = testDetailsMapper.listObjs(testDetailsMapper.query().distinct()
                    .select.apply("t2022_version").end().where.t2022Version().notNull().end());
            for (Object version : versionList) {
                List<Map<String, Object>> queryAll = testDetailsMapper.listMaps(testDetailsMapper.query().distinct()
                        .select.apply(PROVINCE).end().where.t2022Version().eq(version.toString()).end());
                List<Map<String, Object>> queryArgs = testDetailsMapper.listMaps(testDetailsMapper.query().distinct()
                        .select.apply(PROVINCE).end().where.t2022Version().eq(version.toString())
                        .and.t2022Result().in(qArgs).end());
                DecimalFormat df = new DecimalFormat("0.00");
                double tmpVal = Double.parseDouble(df.format((double) (queryArgs.size()) / queryAll.size()));
                // 更新全国通过率
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2022TotalPassRate().is(tmpVal).end()
                        .where.t2022Version().eq(version).end());
            }
            // 更新是否可部署
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2022AllowDeploy().is("是").end()
                    .where.t2022Result().in(qArgs).and.t2022TotalPassRate().eq("1.0").end());
        } else if (year.equals("2021")) {
            List<Object> versionList = testDetailsMapper.listObjs(testDetailsMapper.query().distinct()
                    .select.apply("t2021_version").end().where.t2021Version().notNull().end());
            for (Object version : versionList) {
                List<Map<String, Object>> queryAll = testDetailsMapper.listMaps(testDetailsMapper.query().distinct()
                        .select.apply(PROVINCE).end().where.t2021Version().eq(version.toString()).end());
                List<Map<String, Object>> queryArgs = testDetailsMapper.listMaps(testDetailsMapper.query().distinct()
                        .select.apply(PROVINCE).end().where.t2021Version().eq(version.toString())
                        .and.t2021Result().in(qArgs).end());
                DecimalFormat df = new DecimalFormat("0.00");
                double tmpVal = Double.parseDouble(df.format((double) (queryArgs.size()) / queryAll.size()));
                // 更新全国通过率
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2021TotalPassRate().is(tmpVal).end()
                        .where.t2021Version().eq(version).end());
            }
            // 更新是否可部署
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2021AllowDeploy().is("是").end()
                    .where.t2021Result().in(qArgs).and.t2021TotalPassRate().eq("1.0").end());
        } else if (year.equals("2023")) {
            List<Object> versionList = testDetailsMapper.listObjs(testDetailsMapper.query().distinct()
                    .select.apply("t2023_version").end().where.t2023Version().notNull().end());
            for (Object version : versionList) {
                List<Map<String, Object>> queryAll = testDetailsMapper.listMaps(testDetailsMapper.query().distinct()
                        .select.apply(PROVINCE).end().where.t2023Version().eq(version.toString()).end());
                List<Map<String, Object>> queryArgs = testDetailsMapper.listMaps(testDetailsMapper.query().distinct()
                        .select.apply(PROVINCE).end().where.t2023Version().eq(version.toString())
                        .and.t2023Result().in(qArgs).end());
                DecimalFormat df = new DecimalFormat("0.00");
                double tmpVal = Double.parseDouble(df.format((double) (queryArgs.size()) / queryAll.size()));
                // 更新全国通过率
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2023TotalPassRate().is(tmpVal).end()
                        .where.t2023Version().eq(version).end());
            }
            // 更新是否可部署
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2023AllowDeploy().is("是").end()
                    .where.t2023Result().in(qArgs).and.t2023TotalPassRate().eq("1.0").end());
        }
    }

    public Map<String, Object> lineInfo(String province, List<String> vendor, String status, String version, String gatewayModel, String year, Integer current, Integer size, Long[] ids) throws ParseException {
        log.info(String.format("条件查询省级终端版本提测数据：%s, %s, %s, %s, %s", province, vendor, status, version, year));
        Map<String, Object> mapResult = new HashMap<>();
        TestDetailsQuery query;
        if (Objects.nonNull(ids) && ids.length >= 1) {
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems().t2022FailedItemsNum()
                    .t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems().t2023FailedItemsNum()
                    .t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().
                    where.id().in(ids)
                    .and.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.lineStatus().eq("无").end().orderBy().createTime().desc().end();
        } else if (year.equals("2022")) {
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().
                    where.province().eq(province.equals("内蒙古") ? "内蒙" : province, StrUtil::isNotBlank).
                    and.t2022Result().eq(status, StrUtil::isNotBlank).
                    and.gatewayModel().eq(gatewayModel, StrUtil::isNotBlank).
                    and.t2022Version().eq(version, StrUtil::isNotBlank)
                    .and.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2022Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e22date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else if (year.equals("2021")) {
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().
                    where.province().eq(province.equals("内蒙古") ? "内蒙" : province, StrUtil::isNotBlank).
                    and.t2021Result().eq(status, StrUtil::isNotBlank).
                    and.gatewayModel().eq(gatewayModel, StrUtil::isNotBlank).
                    and.t2021Version().eq(version, StrUtil::isNotBlank)
                    .and.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.lineStatus().eq("无")
                    .and.t2021Version().notNull()
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e21date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else if (year.equals("2023")) {
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().
                    where.province().eq(province.equals("内蒙古") ? "内蒙" : province, StrUtil::isNotBlank).
                    and.t2023Result().eq(status, StrUtil::isNotBlank).
                    and.gatewayModel().eq(gatewayModel, StrUtil::isNotBlank).
                    and.t2023Version().eq(version, StrUtil::isNotBlank)
                    .and.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2023Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e23date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else {
            mapResult.put("msg", WUYEARDATA);
            return mapResult;
        }
        if (!vendor.isEmpty()) {
            query.where.vendor().in(vendor);
        }

        int count = testDetailsMapper.count(query);
        if (count == 0) {
            mapResult.put(DATALIST, null);
            mapResult.put(TOTAL, 0);
            return mapResult;
        }

        List<Object> idList = testDetailsMapper.listObjs(query.select.id().end());
        mapResult.put(IDLIST, idList);
        if (size != -1) {
            query.limit((current - 1) * size, size);
        }
        List<Map<String, Object>> result = testDetailsMapper.listMaps(query);
        List<Object> idsFromResult = result.stream()
                .map(item -> item.get("id"))
                .collect(Collectors.toList());

        IBaseQuery<TestStatusEntity, TestStatusQuery> testStatusQuery = testStatusMapper.query()
                .select.relatedId().opinion().end()
                .where.relatedId().in(idsFromResult)
                .and.year().eq(year).end();
        List<Map<String, Object>> opinionsData = testStatusMapper.listMaps(testStatusQuery);

        Map<Object, Object> opinionMap = new HashMap<>();
        for (Map<String, Object> data : opinionsData) {
            opinionMap.put(data.get("related_id"), data.get("opinion"));
        }
        for (Map<String, Object> item : result) {
            Object id = item.get("id");
            if (opinionMap.containsKey(id)) {
                item.put("opinion", opinionMap.get(id));
            }
        }

        mapResult.put(DATALIST, result);
        mapResult.put(TOTAL, count);
        // 支持测试明细下载时判断是否上传测试明细
        List<Object> filepathlist = testStatusMapper.listObjs(testStatusMapper.query().distinct()
                .select.apply(RELATEDID).end().where.fileinfopath().notNull().end());
        mapResult.put(TESTLIST, filepathlist);
        return mapResult;
    }

    public List<Map<String, Object>> lineAllInfo() {
        log.info("省级终端版本提测数据全量数据导出");
        TestDetailsQuery query;
        query = testDetailsMapper.query().selectAll().where.lineStatus().eq("无").end();
        return testDetailsMapper.listMaps(query);
    }


    public Map<String, Object> provinceStartTestFunc(String year) throws ParseException {
        Map<String, Object> mapResult = new HashMap<>();
        TestDetailsQuery query;
        if (year.equals("2022")) {
            List<Long> ids = testDetailsMapper.listObjs(testDetailsMapper.query().select.id().end()
                    .where.t2022Result().eq(VENDOROVER).end());
            for (Long tdid : ids) {
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2022Result().is(PROVINCETEST)
                        .set.t2022TestTimestamp().is(getNowtime()).end().where.id().eq(tdid).end());
                // 插入测试状态数据
                TestStatusEntity entity = new TestStatusEntity();
                entity.setRelatedId(tdid);
                entity.setYear(year);
                entity.setStage("省内测试");
                entity.setStatus(PROVINCETEST);
                entity.setTestTimestamp(getNowtime());
                testStatusMapper.insert(entity);
            }
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2022Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e22date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else if (year.equals("2021")) {
            List<Long> ids = testDetailsMapper.listObjs(testDetailsMapper.query().select.id().end()
                    .where.t2021Result().eq(VENDOROVER).end());
            for (Long tdid : ids) {
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2021Result().is(PROVINCETEST)
                        .set.t2021TestTimestamp().is(getNowtime()).end().where.id().eq(tdid).end());
                // 插入测试状态数据
                TestStatusEntity entity = new TestStatusEntity();
                entity.setRelatedId(tdid);
                entity.setYear(year);
                entity.setStage("省内测试");
                entity.setStatus(PROVINCETEST);
                entity.setTestTimestamp(getNowtime());
                testStatusMapper.insert(entity);
            }
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2021Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e21date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else if (year.equals("2023")) {
            List<Long> ids = testDetailsMapper.listObjs(testDetailsMapper.query().select.id().end()
                    .where.t2023Result().eq(VERSIONCODE).t2023Version().notNull().end());
            for (Long tdid : ids) {
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2023Result().is(PROVINCETEST)
                        .set.t2023TestTimestamp().is(getNowtime()).end().where.id().eq(tdid).and.t2023Version().notNull().end());
                // 插入测试状态数据
                TestStatusEntity entity = new TestStatusEntity();
                entity.setRelatedId(tdid);
                entity.setYear(year);
                entity.setStage("省内测试");
                entity.setStatus(PROVINCETEST);
                entity.setTestTimestamp(getNowtime());
                testStatusMapper.insert(entity);
            }
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where.vendor().notNull()
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2023Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e23date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else {
            mapResult.put("msg", WUYEARDATA);
            return mapResult;
        }

        int count = testDetailsMapper.count(query);
        List<Object> idList = testDetailsMapper.listObjs(query.select.id().end());
        mapResult.put(IDLIST, idList);
        List<TestDetailsEntity> result = testDetailsMapper.listEntity(query.limit(0, 10));
        mapResult.put(DATALIST, result);
        mapResult.put(TOTAL, count);
        List<Object> filepathlist = testStatusMapper.listObjs(testStatusMapper.query().distinct()
                .select.apply(RELATEDID).end().where.fileinfopath().notNull().end());
        mapResult.put(TESTLIST, filepathlist);
        return mapResult;
    }

    public Map<String, Object> addTestVersionFunc(Long ids, String vendor, String province, String gateway_model,
                                                  String tversion, String year) throws ParseException {
        log.info(String.format("厂家添加测试版本： %s, %s ", ids, tversion));
        Map<String, Object> mapResult = new HashMap<>();

        TestDetailsQuery query;
        if (year.equals("2022")) {
            // 检测表里是否有该条目
            int intobj = testDetailsMapper.count(testDetailsMapper.query().where.vendor().eq(vendor)
                    .and.province().eq(province).and.gatewayModel().eq(gateway_model)
                    .and.t2022Version().eq(tversion).end());
            if (intobj == 1) {
                mapResult.put("msg", "检测出数据库有相同条目数据！");
                return mapResult;
            }
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2022Result().is(VENDOROVER)
                    .set.t2022TestTimestamp().is(getNowtime())
                    .set.t2022Version().is(tversion).end().where.id().eq(ids).end());
            TestStatusEntity entity = new TestStatusEntity();
            entity.setRelatedId(ids);
            entity.setYear(year);
            entity.setStage("厂家测试");
            entity.setStatus(VENDOROVER);
            entity.setTestTimestamp(getNowtime());
            testStatusMapper.insert(entity);

            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.vendor().eq(vendor)
                    .and.t2022Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e22date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else if (year.equals("2021")) {
            // 检测表里是否有该条目
            int intobj = testDetailsMapper.count(testDetailsMapper.query().where.vendor().eq(vendor)
                    .and.province().eq(province).and.gatewayModel().eq(gateway_model)
                    .and.t2021Version().eq(tversion).end());
            if (intobj == 1) {
                mapResult.put("msg", "检测出数据库有相同条目数据！");
                return mapResult;
            }
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2021Result().is(VENDOROVER)
                    .set.t2021TestTimestamp().is(getNowtime())
                    .set.t2021Version().is(tversion).end().where.id().eq(ids).end());
            TestStatusEntity entity = new TestStatusEntity();
            entity.setRelatedId(ids);
            entity.setYear(year);
            entity.setStage("厂家测试");
            entity.setStatus(VENDOROVER);
            entity.setTestTimestamp(getNowtime());
            testStatusMapper.insert(entity);

            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.vendor().eq(vendor)
                    .and.t2021Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e21date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else if (year.equals("2023")) {
            // 检测表里是否有该条目
            int intobj = testDetailsMapper.count(testDetailsMapper.query().where.vendor().eq(vendor)
                    .and.province().eq(province).and.gatewayModel().eq(gateway_model)
                    .and.t2023Version().eq(tversion).end());
            if (intobj == 1) {
                mapResult.put("msg", "检测出数据库有相同条目数据！");
                return mapResult;
            }
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2023Result().is(VENDOROVER)
                    .set.t2023TestTimestamp().is(getNowtime())
                    .set.t2023Version().is(tversion).end().where.id().eq(ids).end());
            TestStatusEntity entity = new TestStatusEntity();
            entity.setRelatedId(ids);
            entity.setYear(year);
            entity.setStage("厂家测试");
            entity.setStatus(VENDOROVER);
            entity.setTestTimestamp(getNowtime());
            testStatusMapper.insert(entity);

            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where
                    .and.province().notNull()
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.vendor().eq(vendor)
                    .and.t2023Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e23date)
                            .or.createTime().isNull().end()).end().orderBy().createTime().desc().end();
        } else {
            mapResult.put("msg", WUYEARDATA);
            return mapResult;
        }

        int count = testDetailsMapper.count(query);
        List<Object> idList = testDetailsMapper.listObjs(query.select.id().end());
        mapResult.put(IDLIST, idList);
        List<TestDetailsEntity> result = testDetailsMapper.listEntity(query.limit(0, 10));
        mapResult.put(DATALIST, result);
        mapResult.put(TOTAL, count);
        List<Object> filepathlist = testStatusMapper.listObjs(testStatusMapper.query().distinct()
                .select.apply(RELATEDID).end().where.fileinfopath().notNull().end());
        mapResult.put(TESTLIST, filepathlist);
        return mapResult;
    }

    public List<TestStatusEntity> testStatusQueryFunc(Long ids, String year) {

        return testStatusMapper.listEntity(testStatusMapper.query()
                .where.relatedId().eq(ids).and.year().eq(year).end().orderBy().testTimestamp().asc().end());
    }

    public Map<String, Object> testResultSubmitFunc(Long ids, String tresult, MultipartFile detailfile,
                                                    String topinion, String cm_contact, String cm_contact_mailbox,
                                                    String cm_contact_phone, String province, String year) throws ParseException, IOException {
        Map<String, Object> mapResult = new HashMap<>();
        // 验证邮箱
        if (!isEmail(cm_contact_mailbox)) {
            mapResult.put("msg", "邮箱格式错误！");
            return mapResult;
        }
        String filePath = null;
        // 插入测试状态数据
        TestStatusEntity entity = new TestStatusEntity();
        entity.setRelatedId(ids);
        entity.setYear(year);
        entity.setStage("省内测试");
        entity.setStatus(tresult);
        entity.setTestTimestamp(getNowtime());
        if (!topinion.isEmpty()) {
            entity.setOpinion(topinion);
        }
        TestDetailsEntity test = testDetailsMapper.findById(ids);
        String vendor = test.getVendor();
        String gatewayModel = test.getGatewayModel();

        String t2022Version = test.getT2022Version();
        String version = year.equals("2022") ? t2022Version : test.getT2023Version();
        // 保存文件到本地 保存文件路径
        if (Objects.nonNull(detailfile) && !detailfile.isEmpty()) {
            filePath = proTerminalTestSaveService.saveFileToLocal(getFileDir(), detailfile, province, vendor, gatewayModel, version);
            entity.setFileinfopath(filePath);
        }
        testStatusMapper.insert(entity);
        TestDetailsQuery query;
        if (year.equals("2022")) {
            // 查询未更新状态表中该id是否有"测试通过"状态Status，如果参数tresult的值为 "测试不通过"，发邮件给总部，总部邮箱
            int testOkObj = testDetailsMapper.count(testDetailsMapper.query().where.id().eq(ids).and.t2022Result().eq("测试通过").end());
            log.info(String.format("数据库里是否有测试通过：%s", testOkObj));
            if (testOkObj == 1 && tresult.equals(TESTERR)) {
                log.info("测试结果由通过变更为不通过同时发送邮件至 厂家 和 总部");
                // 获取总部邮箱待定，发送邮件,暂时为移动测试人邮箱
                TestDetailsEntity idsDbObj = testDetailsMapper.findOne(testDetailsMapper.query().where.id().eq(ids).end());
                String mailBody = String.format("你好!省级终端版本提测中省份为：%s厂商为：%s版本为：%s测试结果通过改为不通过。请知悉！",
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2022Version());
                sendMailService.sendMailFunc("wenke@chinamobile.com", tresult, mailBody); // 需要可以发送邮件环境,直接使用邮箱参数，暂注释

                // 获取厂家邮箱，发送邮件
                String vdContactMailbox = idsDbObj.getVdContactMailbox();
                String vdmailBody = String.format("你好!省级终端版本提测中省份为：%s厂商为：%s版本为：%s测试结果通过变更为不通过。请知悉！",
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2022Version());
                String getVendorEmail = vendorEmailService.getVendorEmail(idsDbObj.getVendor());
                if (!getVendorEmail.equals(vdContactMailbox)) {
                    sendMailService.sendMailFunc(vdContactMailbox, tresult, vdmailBody); // 条目的厂家邮箱和登陆账号的邮箱一致，只发送一次邮件
                }
                sendMailService.sendMailFunc(getVendorEmail, tresult, vdmailBody); // 条目的厂家邮箱和登陆账号的邮箱不一致，发送两次邮件

                // 如果没有"测试通过"，tresult的值为 "测试不通过"，发邮件给厂家
            } else if (tresult.equals(TESTERR)) {
                log.info("发送邮件->不通过的测试结果给厂家");
                // 获取厂家邮箱，发送邮件
                TestDetailsEntity idsDbObj = testDetailsMapper.findOne(testDetailsMapper.query().where.id().eq(ids).end());
                String vdContactMailbox = idsDbObj.getVdContactMailbox();
                String mailBody = String.format(EMAILMSG, idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2022Version());
                String getVendorEmail = vendorEmailService.getVendorEmail(idsDbObj.getVendor());
                if (!getVendorEmail.equals(vdContactMailbox)) {
                    sendMailService.sendMailFunc(vdContactMailbox, tresult, mailBody); // 条目的厂家邮箱和登陆账号的邮箱一致，只发送一次邮件
                }
                sendMailService.sendMailFunc(getVendorEmail, tresult, mailBody); // 条目的厂家邮箱和登陆账号的邮箱不一致，发送两次邮件
            }
            if (tresult.equals(TESTERR) && Objects.nonNull(detailfile) && !detailfile.isEmpty()) {
                // 测试不通过且上传了明细表，
                // 解析detailfile 更新提测结果表,更新未通过项及其全部个数，解析时返回全部不通过项
//                String failedItem = parseDetailFileService.parseTable(detailfile); //文科说先不解析了，
                // 更新提测和结果表数据
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2022Result().is(tresult)
                        .set.t2022TestTimestamp().is(getNowtime())
//                        .set.t2022FailedItemsNum().is(failedItem.split(",").length)
//                        .set.t2022FailedItems().is(failedItem)
                        .set.cmTestContact().is(cm_contact)
                        .set.cmContactMailbox().is(cm_contact_mailbox)
                        .set.cmContactMobile().is(cm_contact_phone).end().where.id().eq(ids).end());
            }
            // tresult是测试通过，未发货无需测试，未提供测试版本，则直接更新提测结果表
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2022Result().is(tresult)
                    .set.t2022TestTimestamp().is(getNowtime())
                    .set.cmTestContact().is(cm_contact)
                    .set.cmContactMailbox().is(cm_contact_mailbox)
                    .set.cmContactMobile().is(cm_contact_phone).end().where.id().eq(ids).end());


            // 触发通过率计算
            statisticsPassRate(year);
            // 返回更新后的提测结果表数据
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where.vendor().notNull()
                    .and.province().eq(province, StrUtil::isNotBlank)
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2022Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e22date)
                            .or.createTime().isNull().end()).end().orderBy().t2022TestTimestamp().desc().end();
        } else if (year.equals("2021")) {
            // 查询未更新状态表中该id是否有"测试通过"状态Status，如果参数tresult的值为 "测试不通过"，发邮件给总部，总部邮箱地址待定
            int testOkObj = testDetailsMapper.count(testDetailsMapper.query().where.id().eq(ids).and.t2021Result().eq("测试通过").end());
            log.info(String.format("数据库里是否有测试通过：%s", testOkObj));
            if (testOkObj == 1 && tresult.equals(TESTERR)) {
                log.info("测试结果由通过变更为不通过同时发送邮件至 厂家 和 总部");
                // 获取总部邮箱待定，发送邮件,暂时为移动测试人邮箱
                TestDetailsEntity idsDbObj = testDetailsMapper.findOne(testDetailsMapper.query().where.id().eq(ids).end());
                String mailBody = String.format(EMAILMSG,
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2021Version());
                sendMailService.sendMailFunc("wenke@chinamobile.com", tresult, mailBody);

                // 获取厂家邮箱，发送邮件
                String vdContactMailbox = idsDbObj.getVdContactMailbox();
                String vdmailBody = String.format(EMAILMSG,
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2021Version());
                String getVendorEmail = vendorEmailService.getVendorEmail(idsDbObj.getVendor());
                if (!getVendorEmail.equals(vdContactMailbox)) {
                    sendMailService.sendMailFunc(vdContactMailbox, tresult, vdmailBody); // 条目的厂家邮箱和登陆账号的邮箱一致，只发送一次邮件
                }
                sendMailService.sendMailFunc(getVendorEmail, tresult, vdmailBody); // 条目的厂家邮箱和登陆账号的邮箱不一致，发送两次邮件

                // 如果没有"测试通过"，tresult的值为 "测试不通过"，发邮件给厂家
            } else if (tresult.equals(TESTERR)) {
                log.info("发送邮件->不通过的测试结果给厂家");
                // 获取厂家邮箱，发送邮件
                TestDetailsEntity idsDbObj = testDetailsMapper.findOne(testDetailsMapper.query().where.id().eq(ids).end());
                String vdContactMailbox = idsDbObj.getVdContactMailbox();
                String mailBody = String.format(EMAILMSG,
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2021Version());
                String getVendorEmail = vendorEmailService.getVendorEmail(idsDbObj.getVendor());
                if (!getVendorEmail.equals(vdContactMailbox)) {
                    sendMailService.sendMailFunc(vdContactMailbox, tresult, mailBody); // 条目的厂家邮箱和登陆账号的邮箱一致，只发送一次邮件
                }
                sendMailService.sendMailFunc(getVendorEmail, tresult, mailBody); // 条目的厂家邮箱和登陆账号的邮箱不一致，发送两次邮件

            }
            if (tresult.equals(TESTERR) && Objects.nonNull(detailfile) && !detailfile.isEmpty()) {
                // 测试不通过且上传了明细表，
                // 解析detailfile 更新提测结果表,更新未通过项及其全部个数，解析时返回全部不通过项
//                String failedItem = parseDetailFileService.parseTable(detailfile);
                // 更新提测和结果表数据
                testDetailsMapper.updateBy(testDetailsMapper.updater()
                        .set.t2021Result().is(tresult)
                        .set.t2021TestTimestamp().is(getNowtime())
//                        .set.t2021FailedItemsNum().is(failedItem.split(",").length)
//                        .set.t2021FailedItems().is(failedItem)
                        .set.cmTestContact().is(cm_contact)
                        .set.cmContactMailbox().is(cm_contact_mailbox)
                        .set.cmContactMobile().is(cm_contact_phone).end().where.id().eq(ids).end());
            } else {
                // tresult是测试通过，未发货无需测试，未提供测试版本，则直接更新提测结果表
                testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2021Result().is(tresult)
                        .set.t2021TestTimestamp().is(getNowtime())
                        .set.cmTestContact().is(cm_contact)
                        .set.cmContactMailbox().is(cm_contact_mailbox)
                        .set.cmContactMobile().is(cm_contact_phone).end().where.id().eq(ids).end());

            }
            // 触发通过率计算
            statisticsPassRate(year);
            // 返回更新后的提测结果表数据
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where.vendor().notNull()
                    .and.province().eq(province, StrUtil::isNotBlank)
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2021Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e21date)
                            .or.createTime().isNull().end()).end().orderBy().t2021TestTimestamp().desc().end();

        } else if (year.equals("2023")) {
            // 查询未更新状态表中该id是否有"测试通过"状态Status，如果参数tresult的值为 "测试不通过"，发邮件给总部，总部邮箱地址待定
            int testOkObj = testDetailsMapper.count(testDetailsMapper.query().where.id().eq(ids).and.t2023Result().eq("测试通过").end());
            log.info(String.format("数据库里是否有测试通过：%s", testOkObj));
            if (testOkObj == 1 && tresult.equals(TESTERR)) {
                log.info("测试结果由通过变更为不通过同时发送邮件至 厂家 和 总部");
                // 获取总部邮箱待定，发送邮件,暂时为移动测试人邮箱
                TestDetailsEntity idsDbObj = testDetailsMapper.findOne(testDetailsMapper.query().where.id().eq(ids).end());
                String mailBody = String.format(EMAILMSG,
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2021Version());
                sendMailService.sendMailFunc("wenke@chinamobile.com", tresult, mailBody);

                // 获取厂家邮箱，发送邮件
                String vdContactMailbox = idsDbObj.getVdContactMailbox();
                String vdmailBody = String.format(EMAILMSG,
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2021Version());
                String getVendorEmail = vendorEmailService.getVendorEmail(idsDbObj.getVendor());
                if (!getVendorEmail.equals(vdContactMailbox)) {
                    sendMailService.sendMailFunc(vdContactMailbox, tresult, vdmailBody); // 条目的厂家邮箱和登陆账号的邮箱一致，只发送一次邮件
                }
                sendMailService.sendMailFunc(getVendorEmail, tresult, vdmailBody); // 条目的厂家邮箱和登陆账号的邮箱不一致，发送两次邮件

                // 如果没有"测试通过"，tresult的值为 "测试不通过"，发邮件给厂家
            } else if (tresult.equals(TESTERR)) {
                log.info("发送邮件->不通过的测试结果给厂家");
                // 获取厂家邮箱，发送邮件
                TestDetailsEntity idsDbObj = testDetailsMapper.findOne(testDetailsMapper.query().where.id().eq(ids).end());
                String vdContactMailbox = idsDbObj.getVdContactMailbox();
                String mailBody = String.format(EMAILMSG,
                        idsDbObj.getProvince(), idsDbObj.getVendor(), idsDbObj.getT2021Version());
                String getVendorEmail = vendorEmailService.getVendorEmail(idsDbObj.getVendor());
                if (!getVendorEmail.equals(vdContactMailbox)) {
                    sendMailService.sendMailFunc(vdContactMailbox, tresult, mailBody); // 条目的厂家邮箱和登陆账号的邮箱一致，只发送一次邮件
                }
                sendMailService.sendMailFunc(getVendorEmail, tresult, mailBody); // 条目的厂家邮箱和登陆账号的邮箱不一致，发送两次邮件

            }
            if (tresult.equals(TESTERR) && Objects.nonNull(detailfile) && !detailfile.isEmpty()) {
                // 测试不通过且上传了明细表，
                // 解析detailfile 更新提测结果表,更新未通过项及其全部个数，解析时返回全部不通过项
                // 更新提测和结果表数据
                testDetailsMapper.updateBy(testDetailsMapper.updater()
                        .set.t2023Result().is(tresult)
                        .set.t2023TestTimestamp().is(getNowtime())
                        .set.cmTestContact().is(cm_contact)
                        .set.cmContactMailbox().is(cm_contact_mailbox)
                        .set.cmContactMobile().is(cm_contact_phone).end().where.id().eq(ids).end());
            }
            // tresult是测试通过，未发货无需测试，未提供测试版本，则直接更新提测结果表
            testDetailsMapper.updateBy(testDetailsMapper.updater().set.t2023Result().is(tresult)
                    .set.t2023TestTimestamp().is(getNowtime())
                    .set.cmTestContact().is(cm_contact)
                    .set.cmContactMailbox().is(cm_contact_mailbox)
                    .set.cmContactMobile().is(cm_contact_phone).end().where.id().eq(ids).end());


            // 触发通过率计算
            statisticsPassRate(year);
            // 返回更新后的提测结果表数据
            query = testDetailsMapper.query().select.id()
                    .vendor().shape().type().gatewayModel().gatewayHardwareVersion().foundry().chipFactory()
                    .cpuModel().wifiChipFactory().wifiChipModel().ramModel().province().createTime().lineStatus()
                    .editLine().checkOpinion().t2021Version().t2021Result().t2021TestTimestamp().t2021FailedItems()
                    .t2021FailedItemsNum().t2021PassToFailFlag().t2021TotalPassRate().t2021AllowDeploy().t2021AllowDeploy()
                    .t2022Version().t2022Result().t2022TestTimestamp().t2022FailedItems()
                    .t2022FailedItemsNum().t2022PassToFailFlag().t2022TotalPassRate().t2022AllowDeploy().t2022AllowDeploy()
                    .t2023Version().t2023Result().t2023TestTimestamp().t2023FailedItems()
                    .t2023FailedItemsNum().t2023PassToFailFlag().t2023TotalPassRate().t2023AllowDeploy().t2023AllowDeploy()
                    .cmTestContact().cmContactMobile().cmContactMailbox().vdContact().vdContactMobile().vdContactMailbox()
                    .end().where.vendor().notNull()
                    .and.province().eq(province, StrUtil::isNotBlank)
                    .and.shape().notNull()
                    .and.type().notNull()
                    .and.gatewayModel().notNull()
                    .and.gatewayHardwareVersion().notNull()
                    .and.chipFactory().notNull()
                    .and.cpuModel().notNull()
                    .and.wifiChipFactory().notNull()
                    .and.wifiChipModel().notNull()
                    .and.ramModel().notNull()
                    .and.t2023Version().notNull()
                    .and.lineStatus().eq("无")
                    .and(new TestDetailsQuery().where.createTime().between(sBaseDate, e23date)
                            .or.createTime().isNull().end()).end().orderBy().t2023TestTimestamp().desc().end();

        } else {
            mapResult.put("msg", WUYEARDATA);
            return mapResult;
        }

        int count = testDetailsMapper.count(query);
        List<Object> idList = testDetailsMapper.listObjs(query.select.id().end());
        mapResult.put(IDLIST, idList);
        List<TestDetailsEntity> result = testDetailsMapper.listEntity(query.limit(0, 10));

        mapResult.put(DATALIST, result);
        mapResult.put(TOTAL, count);
        List<Object> filepathlist = testStatusMapper.listObjs(testStatusMapper.query().distinct()
                .select.apply(RELATEDID).end().where.fileinfopath().notNull().end());
        mapResult.put(TESTLIST, filepathlist);
        return mapResult;
    }
}
